function y = lowpass(x,fs,fc,order)
% USAGE: 
%  y = lowpass(x)
%  filters the data using default filter (6th-order Butterworth with fc =
%  fs/10).
%
%  y = lowpass(x,fs,fc,order)
%  filters the data with sample rate fs at cutoff freuqency fc with 
%  Butterworth filter of order specified by order. Must have Signal
%  Processing Toolbox installed.


%% Get filter coefficients
switch nargin
    case 1 % use default
         a = [1.000000000000000,...
          -4.787135498852133,...
           9.649517728721909,...
         -10.469078892543861,...
           6.441111881008068,...
          -2.129038750030450,...
           0.295172431349155];


        b = 1e-3*[0.008576557073259,...
           0.051459342439556,...
           0.128648356098891,...
           0.171531141465188,...
           0.128648356098891,...
           0.051459342439556,...
           0.008576557073259];
    case 4 % design filter based on inputs
        d = fdesign.lowpass('N,F3dB',order,fc/fs);
        Hd = design(d,'butter');
        coeffs = Hd.todf2t.coefficients;
        b = coeffs{1};
        a = coeffs{2};
    otherwise % display syntax error 
        error('Must use either lowpass(x) or lowpass(x,fs,fc,order). Type help lowpass for details');
end
 
%% perform AA filtering
    y = filter(b,a,x);